home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 March: Reference Library / Dev.CD Mar 97 RL.toast / mac / Technical Documentation / Macintosh Technical Notes / technotes / tn / 1076_MsgTest.hqx / MsgTest / MacMsgTest / MsgTest.c < prev    next >
Encoding:
Text File  |  1996-08-26  |  3.7 KB  |  167 lines

  1. // •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  2. // MsgTest.c
  3. // 
  4. // July 31, 1996
  5. // By Ben Manuto
  6. // 
  7. // © 1996 by Apple Computer, Inc., all rights reserved.
  8. // 
  9. // •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  10.  
  11.  
  12.  
  13. #include    <Types.h>
  14. #include    <LowMem.h>
  15. #include    "Messaging.h"
  16. #include    "MsgTest.h"
  17. #include    "MsgUtil.h"
  18.  
  19.  
  20. // •••••••••• Globals
  21.  
  22. MsgReceiveUPP uppMsgHandler;
  23. MsgCompletionUPP uppSendCompletion;
  24. MsgCompletionUPP uppReceiveCompletion;
  25.  
  26. extern Boolean gSendCompCalled;
  27. extern Boolean gRcvCompCalled;
  28. extern Boolean gAckReceived;
  29. extern SInt32 gBytesReceived;
  30.  
  31.  
  32. // ••••••••••••••••••••••••
  33.  
  34. MsgRecElemPtr NewMsgRecElem(SInt16 cmdBase, UInt16 cmdCount, UInt32 refCon)
  35. {
  36. MsgRecElemPtr theMsgRecElem;
  37.  
  38.     theMsgRecElem = (MsgRecElemPtr) NewPtrClear(sizeof(MsgRecElem));
  39.     
  40.     theMsgRecElem->recFlags = 0;
  41.     theMsgRecElem->recProc = (MsgReceiveUPP) uppMsgHandler;
  42.     theMsgRecElem->recCmdBase = cmdBase;
  43.     theMsgRecElem->recCmdCount = 2;                            // Number of messages types the receive proce can accept.
  44.     theMsgRecElem->recUserData = refCon;
  45.     
  46.     return(theMsgRecElem);
  47. }
  48.     
  49.  
  50. // ••••••••••••••••••••••••
  51.  
  52. MsgPBlkPtr NewMessage(SInt16 cmdValue, UInt32 param1, UInt32 param2, UInt32 msgSize)
  53. {
  54. MsgPBlkPtr theMsg;
  55. SInt32 i;
  56.  
  57.     theMsg = (MsgPBlkPtr) NewPtrClear(sizeof(MsgPBlk));
  58.     if (MemError()) return 0L;                                // If there was an error allocating space, return NIL.
  59.     
  60.     theMsg->msgCmd = cmdValue;
  61.     theMsg->msgParam1 = param1;
  62.     theMsg->msgParam2 = param2;
  63.     
  64.     if (msgSize > 0) {
  65.         theMsg->msgBuffer = NewPtrClear(msgSize);
  66.         if (MemError()) return 0L;                            // If there was an error allocating space, return NIL.
  67.         theMsg->msgReqCount = msgSize;
  68.         
  69.         for (i = 0; i < msgSize; i++)                         // Fill the buffer with sequentially incrementing bytes.
  70.             ((UInt8*)(theMsg->msgBuffer))[i] = i % 256;
  71.     }
  72.     
  73.     theMsg->msgCompletion = (MsgCompletionUPP) uppSendCompletion;
  74.     theMsg->msgUserData = 0L;
  75.     theMsg->msgFlags = 0;
  76.     
  77.     return(theMsg);
  78. }
  79.  
  80.  
  81.  
  82. // ••••••••••••••••••••••••
  83.  
  84. void DeleteMessage(MsgPBlkPtr theMsg)
  85. {
  86.     DisposePtr(theMsg->msgBuffer);
  87.     DisposePtr((Ptr) theMsg);
  88. }
  89.     
  90.     
  91. // ••••••••••••••••••••••••
  92.  
  93. void DeleteMsgRecElem(MsgRecElemPtr theMsgRecElem)
  94. {
  95.     DisposePtr((Ptr) theMsgRecElem);
  96. }
  97.     
  98.     
  99. // ••••••••••••••••••••••••
  100.  
  101. MsgPBlkPtr MessageHandler(MsgRecElemPtr theRecElem, 
  102.                           UInt16 msgCmd, 
  103.                           UInt32 param1, 
  104.                           UInt32 param2)
  105. {
  106. MsgPBlkPtr theMsg;
  107. SInt16 i;
  108.     
  109.     if ((theRecElem->recCmdBase + kAckMsgType) == msgCmd) {        // Is this an Ack message?
  110.         // •••• Normally you could place checks here on param1 to see if the data packet to the PC was complete.
  111.         // For our ack, we define param1 to contain the number bytes actually received by the PC.
  112.         
  113.         gBytesReceived = param1;                                // Set the global for number of bytes the PC received.
  114.         gAckReceived = true;                                    // set the global.
  115.         return(0L);                                                // Return nil and bail.
  116.     }
  117.     
  118.     theMsg = (MsgPBlkPtr) (theRecElem->recUserData);
  119.     
  120.     for (i = 0; i < theMsg->msgParam2; i++) {                    // Clear the buffer for any new data to receive.
  121.         ((UInt8*)(theMsg->msgBuffer))[i] = 0;
  122.     }
  123.     
  124.     theMsg->msgActCount = 0;
  125.     theMsg->msgCompletion = (MsgCompletionUPP) uppReceiveCompletion;
  126.     theMsg->msgUserData = 0L;
  127.     theMsg->msgFlags = 0L;
  128.     
  129.     return(theMsg);
  130. }
  131.  
  132.  
  133. // ••••••••••••••••••••••••
  134.  
  135. MsgPBlkPtr SendCompletion(MsgPBlkPtr theMsg)
  136. {
  137.     gSendCompCalled = true;
  138.     
  139.     return(theMsg);
  140. }
  141.  
  142.  
  143. // ••••••••••••••••••••••••
  144.  
  145. MsgPBlkPtr RcvCompletion(MsgPBlkPtr theMsg)
  146. {
  147.     gRcvCompCalled = true;
  148.  
  149.     return (theMsg);
  150. }
  151.             
  152.  
  153. // •••••••••••••••••••••••••        
  154.  
  155. OSErr InitializeMsgSystem(void)
  156. {
  157. OSErr error = noErr;
  158.  
  159.     uppSendCompletion = NewMsgCompletionProc(SendCompletion);
  160.     uppMsgHandler = NewMsgReceiveProc(MessageHandler);
  161.     uppReceiveCompletion = NewMsgCompletionProc(RcvCompletion);
  162.     
  163.     error = InitMessageSupport();
  164.     
  165.     return error;
  166. }
  167.